package cc.bluemc.modules.adminUser.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.bluemc.modules.adminUser.entity.Permission;
import cc.bluemc.modules.adminUser.entity.PermissionType;
import cc.bluemc.platform.jdbctemplatetool.JdbcTemplateTool;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;

@Service
public class PermissionService {
	
	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private JdbcTemplateTool jdbcTemplateTool;
	
	
	/**
	 * 保存
	 * @param entiy
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly=false)
	public int save(Permission entiy) throws Exception {
		if(null==entiy.getId()){
			return jdbcTemplateTool.save(entiy,"id");
		}else{
			jdbcTemplateTool.update(entiy);
			return entiy.getId();
		}
		
	}
	
	
	/**
	 * 查询所有功能
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	public List<Permission> getAll() throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		return jdbcTemplateTool.listByWhere(null, null, Permission.class);
	}
	/**
	 * 查询所有功能,角色拥有功能时,checked=true
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	public List<Permission> getAll(Integer roleId) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		String sql="SELECT * ,CASE (SELECT COUNT(0) FROM t_role_perm  rp WHERE  rp.roleId=? AND rp.permId=id) WHEN 0 THEN FALSE ELSE TRUE END checked  FROM t_permission";
		return jdbcTemplateTool.list(sql, new Object[]{roleId}, Permission.class);
	}
	/**
	 * 根据roleCode获取可用功能列表
	 * @param roleCode
	 * @return
	 */
	public List<Permission> getAll(String roleCode){
		String sql="SELECT per.* FROM t_permission  per LEFT JOIN t_role_perm rp ON  per.id=rp.permId LEFT JOIN t_role r ON rp.roleId=r.id WHERE r.code=? AND r.usable=1";
		return jdbcTemplateTool.list(sql, new Object[]{roleCode}, Permission.class);
	}
	/**
	 * 查询角色功能
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	public List<Permission> getPermForRole(int roleId) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		String sql ="SELECT * FROM t_permission  p LEFT JOIN t_role_perm rp ON p.id=rp.permId WHERE rp.roleId=? ";
		return jdbcTemplateTool.list(sql, new Object[]{roleId}, Permission.class);
	}
	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	public Permission get(Integer id) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		return jdbcTemplateTool.get(Permission.class, id);
	}
	
	/**
	 * 根据类型查询permission
	 * @param type
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	public List<Permission> getBeansByType(PermissionType type) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		String where = "";
		if(null!=type){
			where="1=1 AND type=?";
		}
		return jdbcTemplateTool.listByWhere(where, new Object[]{type.toString()}, Permission.class);
	}
}
